/*
 * contrib/btree_gist/btree_inet.c
 */
#include "postgres.h"

#include "btree_gist.h"
#include "btree_utils_num.h"
#include "catalog/pg_type.h"
#include "utils/builtins.h"
#include "utils/inet.h"

typedef struct inetkey
{
	double		lower;
	double		upper;
} inetKEY;

/*
** inet 操作
*/
PG_FUNCTION_INFO_V1(gbt_inet_compress);
PG_FUNCTION_INFO_V1(gbt_inet_union);
PG_FUNCTION_INFO_V1(gbt_inet_picksplit);
PG_FUNCTION_INFO_V1(gbt_inet_consistent);
PG_FUNCTION_INFO_V1(gbt_inet_penalty);
PG_FUNCTION_INFO_V1(gbt_inet_same);


static bool fc_gbt_inetgt(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return (*((const double *) fc_a) > *((const double *) fc_b));
}
static bool fc_gbt_inetge(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return (*((const double *) fc_a) >= *((const double *) fc_b));
}
static bool fc_gbt_ineteq(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return (*((const double *) fc_a) == *((const double *) fc_b));
}
static bool fc_gbt_inetle(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return (*((const double *) fc_a) <= *((const double *) fc_b));
}
static bool fc_gbt_inetlt(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	return (*((const double *) fc_a) < *((const double *) fc_b));
}

static int fc_gbt_inetkey_cmp(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	inetKEY    *fc_ia = (inetKEY *) (((const Nsrt *) fc_a)->t);
	inetKEY    *fc_ib = (inetKEY *) (((const Nsrt *) fc_b)->t);

	if (fc_ia->lower == fc_ib->lower)
	{
		if (fc_ia->upper == fc_ib->upper)
			return 0;

		return (fc_ia->upper > fc_ib->upper) ? 1 : -1;
	}

	return (fc_ia->lower > fc_ib->lower) ? 1 : -1;
}


static const gbtree_ninfo tinfo =
{
	gbt_t_inet,
	sizeof(double),
	16,							/* sizeof(gbtreekey16) */
	fc_gbt_inetgt,
	fc_gbt_inetge,
	fc_gbt_ineteq,
	fc_gbt_inetle,
	fc_gbt_inetlt,
	fc_gbt_inetkey_cmp,
	NULL
};


/**************************************************
 * inet 操作
 **************************************************/


Datum gbt_inet_compress(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);
	GISTENTRY  *fc_retval;

	if (fc_entry->leafkey)
	{
		inetKEY    *fc_r = (inetKEY *) palloc(sizeof(inetKEY));
		bool		fc_failure = false;

		fc_retval = palloc(sizeof(GISTENTRY));
		fc_r->lower = convert_network_to_scalar(fc_entry->key, INETOID, &fc_failure);
		Assert(!fc_failure);
		fc_r->upper = fc_r->lower;
		gistentryinit(*fc_retval, PointerGetDatum(fc_r),
					  fc_entry->rel, fc_entry->page,
					  fc_entry->offset, false);
	}
	else
		fc_retval = fc_entry;

	PG_RETURN_POINTER(fc_retval);
}


Datum gbt_inet_consistent(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);
	Datum		fc_dquery = PG_GETARG_DATUM(1);
	StrategyNumber fc_strategy = (StrategyNumber) PG_GETARG_UINT16(2);

	/* Oid		subtype = PG_GETARG_OID(3); */
	bool	   *fc_recheck = (bool *) PG_GETARG_POINTER(4);
	inetKEY    *fc_kkk = (inetKEY *) DatumGetPointer(fc_entry->key);
	GBT_NUMKEY_R fc_key;
	double		fc_query;
	bool		fc_failure = false;

	fc_query = convert_network_to_scalar(fc_dquery, INETOID, &fc_failure);
	Assert(!fc_failure);

	/* 此函数处理的所有情况都是不准确的 */
	*fc_recheck = true;

	fc_key.lower = (GBT_NUMKEY *) &fc_kkk->lower;
	fc_key.upper = (GBT_NUMKEY *) &fc_kkk->upper;

	PG_RETURN_BOOL(gbt_num_consistent(&fc_key, (void *) &fc_query,
									  &fc_strategy, GIST_LEAF(fc_entry), &tinfo, fcinfo->flinfo));
}


Datum gbt_inet_union(PG_FUNCTION_ARGS)
{
	GistEntryVector *fc_entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
	void	   *fc_out = palloc(sizeof(inetKEY));

	*(int *) PG_GETARG_POINTER(1) = sizeof(inetKEY);
	PG_RETURN_POINTER(gbt_num_union((void *) fc_out, fc_entryvec, &tinfo, fcinfo->flinfo));
}


Datum gbt_inet_penalty(PG_FUNCTION_ARGS)
{
	inetKEY    *fc_origentry = (inetKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
	inetKEY    *fc_newentry = (inetKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
	float	   *fc_result = (float *) PG_GETARG_POINTER(2);

	penalty_num(fc_result, fc_origentry->lower, fc_origentry->upper, fc_newentry->lower, fc_newentry->upper);

	PG_RETURN_POINTER(fc_result);
}

Datum gbt_inet_picksplit(PG_FUNCTION_ARGS)
{
	PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
										(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
										&tinfo, fcinfo->flinfo));
}

Datum gbt_inet_same(PG_FUNCTION_ARGS)
{
	inetKEY    *fc_b1 = (inetKEY *) PG_GETARG_POINTER(0);
	inetKEY    *fc_b2 = (inetKEY *) PG_GETARG_POINTER(1);
	bool	   *fc_result = (bool *) PG_GETARG_POINTER(2);

	*fc_result = gbt_num_same((void *) fc_b1, (void *) fc_b2, &tinfo, fcinfo->flinfo);
	PG_RETURN_POINTER(fc_result);
}
